/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.task.evaluation;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import edu.cmu.lti.sepia.data.db.QAJudgmentUnitDAO;
import edu.cmu.lti.sepia.data.db.SQL;
import edu.cmu.lti.sepia.infra.eval.model.QAJudgmentUnit;
import edu.cmu.lti.sepia.infra.run.RunXmlFacade;
import edu.cmu.lti.sepia.infra.run.model.AnswerCandidate;
import edu.cmu.lti.sepia.infra.run.model.SelectionResult;
import edu.cmu.lti.sepia.infra.run.model.SelectionResultSet;


//Ad hoc class for finding out truncation problem
public class TruncationProblemDetector {

  public static void loadPool(SQL sql, String taskNameShort, InputStream is)
          throws SQLException, Exception {
    SelectionResultSet answers = RunXmlFacade.deserializeSelectionResultSet(is);
    StringBuilder sb = new StringBuilder();
    int totalNG=0;
    int total=0;
    int numTopicAffected=0;
    sb.append( "Topic ID, number of mismatched system response, total system response\n" );
    for (String tid : answers.keySet()) {
      SelectionResult sr = answers.get(tid);
      List<QAJudgmentUnit> units = QAJudgmentUnitDAO.findJudgmentUnitsByTopic(sql, taskNameShort, tid);
      Set<String> submittedTexts = new HashSet<String>();
      for ( QAJudgmentUnit u : units ) {
        submittedTexts.add(u.getSystemResponse());
      }
      int ng=0;
      for ( AnswerCandidate ac : sr ) {
        if (!submittedTexts.contains(ac.getText())) {
          ng++;
          totalNG++;
          //System.out.println("AC: "+ac.getText());
        }
      }
      if (ng>0) {
        numTopicAffected++;
      }
      total += sr.size();
      sb.append( tid+"\t"+ng+" / "+sr.size()+"\n" );
    }
    sb.append( "OVERALL\t"+totalNG+" / "+total+"\n" );
    sb.append( "Affected "+numTopicAffected+" topics.\n" );
    System.out.println(sb);
  }

  public static void main(String[] args) throws Exception {
    File dir = new File("C:\\Users\\hideki\\Desktop\\aclia\\truncation-problem\\submitted formal run");
    SQL sql = new SQL();
    int counter=0;
    for ( File f : dir.listFiles() ) {
      if (f.getName().indexOf("JA")!=-1) { 
        System.out.println(f.getName());
        loadPool(sql, "ja_formal", new FileInputStream(f));
        if (counter++==6)break;
      }
    }
    // loadPool( sql, "cs_formal", "Formal run pool", new FileInputStream(new File(dir,
    // "cs-pool.xml") ) );
    // loadPool( sql, "ct_formal", "Formal run pool", new FileInputStream(new File(dir,
    // "ct-pool.xml") ) );
    sql.close();
  }

}
